﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Commlibrary;

namespace StockAnalyse
{
    public partial class frmMain : DevComponents.DotNetBar.Office2007Form
    {
        private DS_Stock m_dsStock = null;
        private StockHelp m_oStockHelp = new StockHelp();

        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            // 正在读取Stock初始信息...
            this.OutputLog(Const.MSG_00001);
            this.m_dsStock = this.m_oStockHelp.InitDBData();

            if (this.m_dsStock == null)
                this.m_dsStock = new DS_Stock();

            if (this.m_dsStock.M_STOCK.Rows.Count == 0)
            {
                // 未发现M_STOCK初始信息，请重新获取!
                this.OutputLog(Const.MSG_00002);
            }
            /*
            if (this.m_dsStock.M_STOCK_HISTORY.Rows.Count == 0)
            {
                // 未发现M_STOCK_HISTORY初始信息，请重新获取!
                this.OutputLog(Const.MSG_00003);
            }
            if (this.m_dsStock.T_USER_STOCK.Rows.Count == 0)
            {
                // 未发现T_USER_STOCK初始信息!
                this.OutputLog(Const.MSG_00004);
            }
            */
            // 读取初始信息结束.
            this.OutputLog(Const.MSG_00005);

            InitPageAnalyse();
        }

        private void OutputLog(String sLogString)
        {
            OutputLog(sLogString, true, true);
        }

        private void OutputLog(String sLogString, Boolean bAddDate, Boolean bAddReturn)
        {
            // 是否输出日期
            if (bAddDate)
            {
                sLogString = 
                    System.DateTime.Now.ToString(Const.DATE_FMT_YYMMDDHHMMSS) + " " + sLogString;
            }
            // 是否输出换行
            if (bAddReturn)
            {
                sLogString += "\r\n";
            }

            // 输出正文
            this.txtOutput.AppendText(sLogString);
            // 始终滚动到最后
            this.txtOutput.SelectionStart = this.txtOutput.Text.Length - 1;
            this.txtOutput.ScrollToCaret();
            this.txtOutput.Update();
        }

        private void btnInitMStock_Click(object sender, EventArgs e)
        {
            if (this.m_dsStock == null)
                this.m_dsStock = new DS_Stock();

            // 开始重新获取M_STOCK信息，请等待...
            this.OutputLog(Const.MSG_00008, true, false);

            try
            {
                DS_Stock.M_STOCKDataTable dtMStock = this.m_oStockHelp.GetMStockData();
                if (dtMStock != null && dtMStock.Rows.Count > 0)
                {
                    this.m_dsStock.M_STOCK.Rows.Clear();
                    this.m_dsStock.M_STOCK.Merge(dtMStock);
                    // 读取成功！
                    this.OutputLog(Const.MSG_00006, false, true);
                }
                else
                {
                    // 读取失败!
                    this.OutputLog(Const.MSG_00007, false, true);
                }
            }
            catch {
                // 读取失败!
                this.OutputLog(Const.MSG_00007, false, true);
            }
        }

        private void btnInitMStockHistory_Click(object sender, EventArgs e)
        {
            if (this.m_dsStock.M_STOCK.Rows.Count == 0)
            { 
                // 未发现M_STOCK初始信息，请重新获取!
                this.OutputLog(Const.MSG_00002);
                return;
            }

            if (this.m_dsStock == null)
                this.m_dsStock = new DS_Stock();

            this.m_oStockHelp.DelMStockHistoryData();

            // 开始重新获取M_STOCK_HISTORY信息，请等待...
            this.OutputLog(Const.MSG_00009);
            this.OutputLog(
                String.Format(
                    Const.MSG_00012, this.m_dsStock.M_STOCK.Rows.Count.ToString()));

            this.m_dsStock.M_STOCK_HISTORY.Rows.Clear();
            int iLoop = 1;
            foreach (DS_Stock.M_STOCKRow rowMStock in this.m_dsStock.M_STOCK.Rows)
            {
                try
                {
                    // 正在读取{0}:{1}的{2}天数据...
                    this.OutputLog(
                        String.Format(
                            Const.MSG_00010,
                            iLoop.ToString(),
                            rowMStock.STOCK_NO,
                            rowMStock.STOCK_NAME,
                            Const.MAX_HISTORY_CNT),
                        true,
                        false);
                    // 读取信息
                    if (this.m_oStockHelp.GetMStockHistoryData(rowMStock.STOCK_NO))
                    {
                        // 读取成功!
                        this.OutputLog(Const.MSG_00006, false, true);
                    }
                    else
                    {
                        // 读取失败!
                        this.OutputLog(Const.MSG_00007, false, true);
                    }
                }
                catch (Exception)
                {
                    // 读取失败!
                    this.OutputLog(Const.MSG_00007, false, true);
                }

                iLoop++;
            }

            // 获取M_STOCK_HISTORY信息结束.
            this.OutputLog(Const.MSG_00011);
        }

        private void btnConfig_Click(object sender, EventArgs e)
        {
            using (frmConfig oFrmConfig = new frmConfig())
            {
                oFrmConfig.ShowDialog();
            }
        }

        private void btnAnalyse_Click(object sender, EventArgs e)
        {

        }

        private void btnInitStockWorkData_Click(object sender, EventArgs e)
        {
            int iPiece = 1;
            if (this.txtBlockNo.Text.Trim().Length > 0)
            {
                if (CommonCheck.isInteger(this.txtBlockNo.Text))
                {
                    iPiece = Int32.Parse(this.txtBlockNo.Text);
                    if (iPiece > 10) iPiece = 10;
                    if (iPiece < 1) iPiece = 1;

                    this.m_oStockHelp.DelMStockWorkData();

                    // 开始生成工作表数据，请等待..
                    this.OutputLog(Const.MSG_00013, true, false);
                    if (this.m_oStockHelp.PrepareStockWorkData("", iPiece))
                    {
                        // 生成成功!
                        this.OutputLog(Const.MSG_00014, false, true);
                    }
                    else{
                        // 生成失败!
                        this.OutputLog(Const.MSG_00015, false, true);
                    }
                }
            }

            InitPageAnalyse();
        }

        /// <summary>
        /// 画面终了
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SetPageAnalyseEnabled(Boolean bEnabled)
        {
            // 历史数据范围
            this.txtHistoryRangeStart.Enabled = bEnabled;
            this.txtHistoryRangeEnd.Enabled = bEnabled;

            // 价格范围
            this.txtPriceRangeStart.Enabled = bEnabled;
            this.txtPriceRangeEnd.Enabled = bEnabled;

            // 比较日期
            this.txtCmpDate.Enabled = bEnabled;

            // 显示件数
            this.txtDisplayCnt.Enabled = bEnabled;

            // 查找
            this.btnDisplay.Enabled = bEnabled;
        }

        private void InitPageAnalyse()
        {
            DateTime[] dtMinMax = this.m_oStockHelp.GetStockHistoryMinMaxDate();

            if (dtMinMax == null)
            {
                this.SetPageAnalyseEnabled(false);
                return;
            }
            else
            {
                this.SetPageAnalyseEnabled(true);
            }

            DateTime dtMinDate = dtMinMax[0];
            DateTime dtMaxDate = dtMinMax[1];

            // 历史数据范围
            this.txtHistoryRangeStart.Value = dtMinDate;
            this.txtHistoryRangeStart.MinDate = dtMinDate;
            this.txtHistoryRangeStart.MaxDate = dtMaxDate.AddDays(-1);
            this.txtHistoryRangeEnd.Value = dtMaxDate;
            this.txtHistoryRangeEnd.MinDate = dtMinDate.AddDays(1);
            this.txtHistoryRangeEnd.MaxDate = dtMaxDate;

            // 价格范围
            this.txtPriceRangeStart.Text = "0";
            this.txtPriceRangeEnd.Text = "10";

            // 比较日期
            this.txtCmpDate.Value = DateTime.Now;
            this.txtCmpDate.MinDate = dtMinDate;
            this.txtCmpDate.MaxDate = DateTime.Now;
        }

        private void tbMain_SelectedTabChanged(object sender, DevComponents.DotNetBar.TabStripTabChangedEventArgs e)
        {
            if (this.tbMain.SelectedTab == this.pgInit)
            {
                this.pgPanel1.Show();
                this.pgPanel2.Hide();
            }
            else if (this.tbMain.SelectedTab == this.pgAnalyse)
            {
                this.pgPanel1.Hide();
                this.pgPanel2.Show();
            }
        }
    }
}
